<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use DB;

 use App\Org\Image;

class GoodsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //保存搜索条件
        $where = '';
        //实例化操作表
        $ob = DB::table('detail');
        //判断是否有搜索条件
        if($request->has('title')){
            //获取搜索的条件
            $title = $request->input('title');
            //将添加到将要携带到分页中的数组中
            $where['title']=$title;
            //该查询添加where条件
            $ob->where('title','like',"%{$title}%");
        }
        //执行分页查询
        $list = $ob->paginate(5);
        // dd($list);
        return view('admin.goods.index',['list'=>$list,'where'=>$where]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.goods.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $message = [
        'title.required'=>'商品名必须填写',
        'price.required'=>'价格必须填写',
        'summary.required' => '商品描述必须填写',
        'pic.required' => '商品图片必须上传',
        'price.numeric' => '商品价格必须为数字',
        ];

        $this->validate($request,[
            'title' => 'required',
            'price' => 'required|numeric',
            'summary' => 'required',
            'pic' => 'required',
            ],$message);
            
            // dd($data);
            if($request->hasFile('pic')){
            // 判断上传的文件是否有效
            if($request->file('pic')->isValid()){
                // 获取上传的文件对象
                $data = $request->file('pic');
                // dd($data);
                // 获取上传的文件的后缀
                $ext = $data->getClientOriginalExtension();
                // 拼装出你需要使用的文件名
                $pic = time().rand(1000,9999).'.'.$ext;
                // 移动临时文件，生成新文件到指定目录下
                $data->move('./admin/upload',$pic);
                $img = new Image();
                $img->open("admin/upload/".$pic)->thumb(350,350)->save("admin/upload/mid_".$pic);
                $img->open("admin/upload/".$pic)->thumb(800,800)->save("admin/upload/".$pic);
                $img->open("admin/upload/".$pic)->thumb(60,60)->save("admin/upload/small_".$pic);
                
                }
                
            }else{
                return redirect("admin/goods")->with('error','图片不符合要求');
            }
            

            $data = $request->except('_token');
            $data['pic'] = $pic;
            $data['posttime'] = time();
            // dd($data);
            $id = DB::table('detail')->insertGetId($data);
            if($id>0){
                return redirect('admin/goods')->with('msg','添加成功');
            }
    }


    /*
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        // $data = DB::table('detail')->where('id',$id)->first();
        // dd($data);
        // $type = DB::table('type')->where('id',$data['cid'])->
        $data = DB::table('detail')
            ->join('type', 'type.id', '=', 'detail.cid')
            ->select('detail.*', 'type.name')
            ->where('detail.id',$id)
            ->get()
            ->first();
            // dd($data);
        return view('admin.goods.edit',['ob'=>$data]);

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // dd($request);
        if($request->hasFile('pic')){
            // 判断上传的文件是否有效
            if($request->file('pic')->isValid()){
                // 获取上传的文件对象
                $data = $request->file('pic');
                // dd($data);
                // 获取上传的文件的后缀
                $ext = $data->getClientOriginalExtension();
                // 拼装出你需要使用的文件名
                $pic = time().rand(1000,9999).'.'.$ext;
                // 移动临时文件，生成新文件到指定目录下
                $data->move('./admin/upload',$pic);
                $img = new Image();
                $img->open("admin/upload/".$pic)->thumb(350,350)->save("admin/upload/mid_".$pic);
                $img->open("admin/upload/".$pic)->thumb(800,800)->save("admin/upload/".$pic);
                $img->open("admin/upload/".$pic)->thumb(60,60)->save("admin/upload/small_".$pic);
                
                
                }
            }else{
                return redirect("admin/goods")->with('error','图片不符合要求');
            }
            $data = $request->only('title','price','summary','pic');
            $data['pic'] = $pic;
            // $data['posttime'] = time();
            // dd($data);
            $id = DB::table('detail')->where('id',$id)->update($data);
            if($id>0){
                return redirect('admin/goods')->with('msg','修改成功');
            }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $row = DB::table('detail')->where('id',$id)->delete();
        if($row>0){
            return redirect('admin/goods')->with('msg','删除成功');
        }else{
            return redirect('admin/goods')->with('msg','删除失败');
        }
    }

     public function doget(Request $request)
    {
         // dd(11111);
        $list = DB::table('type')->where('upid',$request->input('upid'))->get();
        echo json_encode($list);
    }
    public function dopost(Request $request)
    {
        $list = DB::table('type')->where('upid',$request->input('upid'))->get();
        echo json_encode($list);
    }
}
